<?php
session_start();
$par_agente = $_GET["agente"];
$par_fechadia = $_GET["fechadia"];
$page = $_GET['page']; // get the requested page

if (!isset($_GET['rows']) && empty($_GET['rows'])){
	if(isset($_GET['nocarga'])){
		$limit = 0;
	}else{
		$limit = 10;
	}
}else{
	if(isset($_GET['nocarga'])){
		$limit = 0;
	}else{
		$limit = $_GET['rows'];
	}
	
}

//$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1;
require_once("../templates/comun.php");
$conexion=Obtener_Conexion();


$sql=" SELECT count(*) as count FROM lth_calendario lth INNER JOIN cxcagentes a on (lth.agente = a.agente) 
	WHERE a.agente = ".$par_agente." and fecha_v='".$par_fechadia."'";
$res=pg_query($conexion,$sql)  or die ("Error $sql");
if (pg_num_rows($res)>0 && !isset($_GET['nocarga'])){
	$row = pg_fetch_array($res);
	$count = $row['count'];
}else{
	$count = 0;
}


if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}

if ($page > $total_pages) $page=$total_pages;

if (!isset($_GET['rows']) && empty($_GET['rows'])){
	$start = 0;
}else{
	$start = $limit*$page - $limit; // do not put $limit*($page - 1)
}




$sql = "SELECT a.nombre as nombreagente, lth.numcte, lth.numsocio,lth.fecha_v as fecha, lth.estatus, lth.agente as agente
		FROM lth_calendario lth 
		INNER JOIN cxcagentes a on (lth.agente = a.agente) 
		WHERE a.agente = ".$par_agente." and fecha_v='".$par_fechadia."' 
		ORDER BY $sidx $sord OFFSET $start LIMIT $limit 
	";

$res=pg_query($conexion,$sql)  or die ("Error $sql");

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";

echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = pg_fetch_array($res)) {
	echo "<row id='". $row[agente]."'>";			
	echo "<cell> <![CDATA[". $row[nombreagente]."]]></cell>";
	echo "<cell>". $row[numcte]."</cell>";
	echo "<cell>". $row[numsocio]."</cell>";
	echo "<cell><![CDATA[". $row[estatus]."]]></cell>";
	echo "<cell>". $row[fecha]."</cell>";
	echo "</row>";
}
echo "</rows>";		
?>
